package com.mj.linkedlist;

/**
 * @Author: LDeng
 * @Date: 2021-02-19 16:01
 */
public abstract class AbstractList<E> implements List<E>{

    protected int size;

    public int size() { return size; }

    public boolean isEmpty() { return size==0; }

    public boolean contains(E element) { return indexOf(element)!= ELEMENT_NOT_FOUND; }

    public void add(E element) { add(size,element); }


    protected void outOfBounds(int index) {
        throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size);
    }

    protected void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    protected void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }
}
